package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.bo.MemberCardHistoryBO;
import com.junzhiit.nengji.dao.MemberCardHistoryDAO;
import com.junzhiit.nengji.model.MemberCardHistory;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.MemberCardHistoryStatus;

@Repository
public class MemberCardHistoryDAOImpl extends AbstractBaseDAO<MemberCardHistory> implements MemberCardHistoryDAO {

	@Override
	public List<MemberCardHistoryBO> queryForList(MemberCardHistoryBO memberCardHistoryBO,int pageNumber, int maxPageRow) {
		StringBuffer sql=new StringBuffer();
		StatementParameter params=new StatementParameter();
		sql.append("select mch.*, sc.card_number,u.name,u.mobile_phone,mc.card_name,mc.price,mc.state");
		sql.append(" from member_card_history mch");
		sql.append(" inner join shop_client sc on mch.shop_client_id=sc.id");
		sql.append(" inner join user u on sc.user_id=u.id");
		sql.append(" inner join member_card mc on mch.member_card_id=mc.id");
		sql.append(" where mch.is_delete=? and mch.cs_id=? and mch.created_time between ? and ?");
		
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(memberCardHistoryBO.getCsId());
		params.setDate(DateUtil.getDateStart(DateUtil.parseDate(memberCardHistoryBO.getStartTime())));
		params.setDate(DateUtil.getDateEnd(DateUtil.parseDate(memberCardHistoryBO.getEndTime())));
		
		if(StringUtils.isNotBlank(memberCardHistoryBO.getTransactionNumber())){
			sql.append(" and mch.transaction_number like ?");
			params.setString("%"+memberCardHistoryBO.getTransactionNumber()+"%");
		}
		if(StringUtils.isNoneBlank(memberCardHistoryBO.getCardNumber())){
			sql.append(" and sc.card_number like ?");
			params.setString("%"+memberCardHistoryBO.getCardNumber()+"%");
		}
		if(memberCardHistoryBO.getStatus() != null){
			sql.append(" and mch.status = ?");
			params.setInt(memberCardHistoryBO.getStatus());
		}
		return super.queryForPagingList(sql.toString(), MemberCardHistoryBO.class, params, pageNumber, maxPageRow);
	}

	@Override
	public int queryForTotal(MemberCardHistoryBO memberCardHistoryBO) {
		StringBuffer sql=new StringBuffer();
		StatementParameter params=new StatementParameter();
		sql.append("select count(mch.id)");
		sql.append(" from member_card_history mch");
		sql.append(" inner join shop_client sc on mch.shop_client_id=sc.id");
		sql.append(" inner join user u on sc.user_id=u.id");
		sql.append(" inner join member_card mc on mch.member_card_id=mc.id");
		sql.append(" where mch.is_delete=? and mch.cs_id=? and mch.created_time between ? and ?");
		
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(memberCardHistoryBO.getCsId());
		params.setDate(DateUtil.getDateStart(DateUtil.parseDate(memberCardHistoryBO.getStartTime())));
		params.setDate(DateUtil.getDateEnd(DateUtil.parseDate(memberCardHistoryBO.getEndTime())));
		
		if(StringUtils.isNotBlank(memberCardHistoryBO.getTransactionNumber())){
			sql.append(" and mch.transaction_number like ?");
			params.setString("%"+memberCardHistoryBO.getTransactionNumber()+"%");
		}
		if(StringUtils.isNoneBlank(memberCardHistoryBO.getCardNumber())){
			sql.append(" and sc.card_number like ?");
			params.setString("%"+memberCardHistoryBO.getCardNumber()+"%");
		}
		if(memberCardHistoryBO.getStatus() != null){
			sql.append(" and mch.status = ?");
			params.setInt(memberCardHistoryBO.getStatus());
		}
		return super.queryForInt(sql.toString(),params);
	}

	@Override
	public MemberCardHistoryBO findForTotal(String csId) {
		StringBuffer sql=new StringBuffer();
		StatementParameter params=new StatementParameter();
		sql.append("select count(mch.id) total_times,count(distinct mch.shop_client_id) total_users,sum(mc.price) total_money");
		sql.append(" from member_card_history mch");
		sql.append(" inner join member_card mc on mch.member_card_id=mc.id");
		sql.append(" where mch.is_delete=? and mch.cs_id=? and mch.status = ?");
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(csId);
		params.setInt(Is.YES.toIntValue());
		return super.query(sql.toString(), MemberCardHistoryBO.class, params);
	}

	@Override
	public MemberCardHistory queryByOrderNumber(String orderNumber) {
		StringBuffer sql=new StringBuffer();
		StatementParameter params=new StatementParameter();
		
		sql.append(" select * from member_card_history where order_number = ? and status = ? and is_delete= ? ");
		params.setString(orderNumber);
		params.setInt(MemberCardHistoryStatus.WAIT.toIntValue());
		params.setInt(IsDelete.NO.toIntValue());
		
		return super.query(sql.toString(),MemberCardHistory.class,params);
	}

}
